package org.jasper.example.sudoku;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;

/**
 * Created by OP038074 on 2018/6/7.
 */
public class SudokuGridView extends JComponent implements ComponentListener {
    private GridLayout gridLayout;
    private JTextField[][] textFields;

    public SudokuGridView() {
        gridLayout = new GridLayout(SudokuGridModel.NUM_ROWS, SudokuGridModel.NUM_COLS);
        setLayout(gridLayout);
        textFields = new JTextField[SudokuGridModel.NUM_ROWS][SudokuGridModel.NUM_COLS];
        for (int row = 0; row < textFields.length; row++) {
            for(int col = 0; col < textFields[row].length; col++) {
                JTextField textField = new JTextField("");
                textField.setEditable(false);
                if (row == 0 && col == 0) {
                    textField.addComponentListener(this);
                }
                JPanel panel = new JPanel();

                int top = row % SudokuGridModel.INNER_GRID_HEIGHT == 0 ? 2 : 0;
                int left = col % SudokuGridModel.INNER_GRID_WIDTH == 0 ? 2 : 0;
                int bottom = row == SudokuGridModel.NUM_ROWS - 1 ? 2 : 0;
                int right = col == SudokuGridModel.NUM_COLS - 1 ? 2 : 0;

                panel.setBorder(BorderFactory.createMatteBorder(top, left, bottom, right, Color.BLUE));
                panel.setLayout(new BorderLayout());

                textField.setOpaque(true);
                textField.setBackground(Color.WHITE);
                textField.setHorizontalAlignment(JTextField.CENTER);

                textFields[row][col] = textField;

                panel.add(BorderLayout.CENTER,textField);
                add(panel);
            }

        }
    }

    public void setModel(SudokuGridModel model){

    }

    @Override
    public void componentResized(ComponentEvent e) {

    }

    @Override
    public void componentMoved(ComponentEvent e) {

    }

    @Override
    public void componentShown(ComponentEvent e) {

    }

    @Override
    public void componentHidden(ComponentEvent e) {

    }
}
